package com.ln.property_control.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ln.property_control.mapper.ClaimMapper;
import com.ln.property_control.pojo.ClaimVo;

import com.ln.property_control.pojo.Department;
import com.ln.property_control.pojo.PropertyVo;
import com.ln.property_control.service.ClaimService;
import com.ln.property_control.utils.GetId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClaimServiceImpl implements ClaimService {
    @Autowired
    private ClaimMapper claimMapper;
    @Override
    public Map<String, Object> findAllByBorrow(Integer page, Integer limit) {
        Page<ClaimVo> objects = PageHelper.startPage(page, limit);
        //数据
        List<ClaimVo> claimList = claimMapper.findAllByBorrow();
        //页数
        long total = objects.getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("data", claimList);
        map.put("count", total);
        map.put("code", 0);
        return map;
    }

    @Override
    public ClaimVo selectByBorrowNumber(String borrowNumber) {
        ClaimVo claimVo = claimMapper.selectByBorrowNumber(borrowNumber);
        return claimVo;
    }

    @Override
    public List<Department> departmentByParentId(int parentId) {
        List<Department> departments = claimMapper.departmentByParentId(parentId);
        return departments;
    }

    @Override
    public String insertByClaim(ClaimVo claimVo) {
        String borrowNumber = "LY"+GetId.getTime().replaceAll("[[\\s-:punct:]]", "");
        claimVo.setBorrowNumber(borrowNumber);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        claimVo.setDrawingTime(simpleDateFormat.format(new Date()));
        int i = claimMapper.insertByClaim(claimVo);
        if(i>0){
            return "ok";
        }else{
            return "on";
        }
    }
    @Override
    public String insertByDepProMidd(String claim_id, List<String>property_id) {
        int i = claimMapper.insertByDepProMidd(claim_id, property_id);
        if(i>0){
            return "ok";
        }else {
            return "on";
        }
    }


    @Override
    public List<PropertyVo> findAllByProperty(PropertyVo propertyVo) {
        if (propertyVo.getProperty_status() == 0) {
            List<PropertyVo> allByProperty = claimMapper.findAllByProperty(propertyVo);
            return allByProperty;
        } else {
            return null;
        }
    }

    @Override
    public int saveByClaim(ClaimVo claimVo) {
        int i = claimMapper.saveByClaim(claimVo);
        return i;
    }

    //返回
    @Override
    public Map<String,Object>findAllByStatus(Integer page, Integer limit,String status){
    Page<ClaimVo> objects = PageHelper.startPage(page, limit);
    //数据
        List<ClaimVo> allByStatus = claimMapper.findAllByStatus(status);
        //页数
    long total = objects.getTotal();

    Map<String, Object> map = new HashMap<>();
        map.put("data", allByStatus);
        map.put("count", total);
        map.put("code", 0);
        return map;
    }
    //返回
    @Override
    public Map<String,Object>findAllByHistory(Integer page, Integer limit,String status){
        Page<ClaimVo> objects = PageHelper.startPage(page, limit);
        //数据
        List<ClaimVo> allByStatus = claimMapper.findAllByHistory(status);
        //页数
        long total = objects.getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("data", allByStatus);
        map.put("count", total);
        map.put("code", 0);
        return map;
    }

    @Override
    public List<PropertyVo> findAllBypropertyVo() {
        List<PropertyVo> allBypropertyVo = claimMapper.findAllBypropertyVo();
        return allBypropertyVo;
    }

    @Override
    public List<PropertyVo> findByProperId(String property_dep_id) {
        List<PropertyVo> byProperId = claimMapper.findByProperId(property_dep_id);
        return byProperId;
    }

    @Override
    public List<ClaimVo> findByProClaimID(String borrowNumber) {
        List<ClaimVo> byProClaimID = claimMapper.findByProClaimID(borrowNumber);
        return byProClaimID;
    }

}